<layout name="page" />
<block name="content">
	<input type="hidden" name="ajax" id="ajax" value="1">
	<select name="dept_list" id="dept_list" class="hidden">
		<option value="0">根节点</option>
		{:fill_option($dept_list)}
	</select>
	{:widget('page_header/simple',array('name'=>'部门管理','search'=>'N'))}
	<div class="operate-panel">
		<i class="mid"></i>
		<a class="btn btn-normal" onclick="add();">新增</a>
		<a class="btn btn-normal" onclick="save()">保存</a>
		<i class="split"></i>
		<a class="btn btn-del" onclick="del()">删除</a>
	</div>
	<div class="x1 sub box">
		<div class="w28 m100 frame" style="max-height:500px;overflow-y: auto;">
			<div class="tree-menu">
				{:widget('html/tree_menu',array($tree_menu))}
			</div>
		</div>
		<div style="width:16px;"></div>
		<div class="x1 frame m100" style="height:500px;">
			<form id="form_data" name="form_data" method="post" style="width:100%;">
				<input type="hidden" name="id" id="id">
				<input type="hidden" name="opmode" id="opmode" value="">
				<div class="col-12 box hidden">
					<label class="input-label" for="dept_no">编码*：</label>
					<input class="input w19" type="text" id="dept_no" name="dept_no" >
				</div>
				<div class="col-12 box">
					<label class="input-label" for="name">名称*：</label>
					<input class="input w19" type="text" id="name" name="name" check="required" info="请输入名称">
				</div>
				<div class="col-12 box">
					<label class="input-label" for="short">简称*：</label>
					<input class="input w19" type="text" id="short" name="short" check="required" info="请输入简称">
				</div>
				<div class="col-12 box">
					<label class="input-label" for="dept_name">上级部门*：</label>
					<div class="input-box w19">
						<input name="pid" id="pid" type="hidden" info="请选择上级部门" check="required" />
						<input name="dept_name" class="input" id="dept_name" type="text" readonly="readonly" info="请选择上级部门" check="required"/>
						<a class="add-on w6" onclick="select_pid();">选择</a>
					</div>
				</div>
				<div class="col-12 box">
					<label class="input-label" for="dept_grade_id">部门级别*：</label>
					<select name="dept_grade_id" id="dept_grade_id" class="select w19" info="请选择部门级别" check="required">
						<option value="">选择部门级别</option>
						{:fill_option($dept_grade_list)}
					</select>
					<i class="icon"></i>
				</div>
				<div class="col-12 box">
					<label class="input-label" for="sort">排序：</label>
					<input class="input w10" type="text" id="sort" name="sort" >
				</div>
				<div class="col-12 box">
					<label class="input-label" for="is_del">状态*：</label>
					<select   name="is_del" id="is_del" class="select w10">
						<option value="0">启用</option>
						<option value="1">禁用</option>
					</select>
					<i class="icon"></i>
				</div>
				<div class="col-12 box">
					<label class="input-label" for="remark" >其他：</label>
					<textarea class="textarea" name="remark" id="remark" rows="5" ></textarea>
				</div>
			</form>
		</div>
	</div>
</block>
<block name="js">
	<script type="text/javascript">
		layui.use(['global'], function() {
			var fn = {
				add : function() {
					winopen("{:url('add')}", 560, 470);
				},
				save : function() {
					send_form("form_data", "{:url('save')}", "{:url('index')}", function(ret) {
						if (ret.status) {
							layer.msg(ret.info, {
								time : 1200
							}, function() {
								location.reload(true);
							});
						} else {
							layer.msg(ret.info);
							return false;
						}
					});
				},
				del : function() {
					if ($(".tree-menu a.active").length == 0) {
						layer.msg('请选择要删除的部门');
						return false;
					}
					if ($(".tree-menu a.active + input").length > 0) {
						layer.msg('有子部门不能删除');
						return false;
					}
					layer.msg('确定要删除吗?', {
						time : 0,
						btn : ['确定', '取消'],
						yes : function(index) {
							var vars = $("#form_data").serialize();
							send_ajax("{:url('del')}", vars, function(ret) {
								if (ret.status) {
									layer.msg(ret.info, {
										time : 1200
									}, function() {
										location.reload(true);
									});
								}
							});
						},
						no : function(index) {
							layer.close(index);
						}
					});
				},
				select_pid : function() {
					if ($(".tree-menu a.active").length > 0) {
						winopen("{:url('select_pid')}", 560, 470);
					} else {
						layer.msg('请选择部门');
					}
				},
			};
			win_exp(fn);

			$('.tree-menu a').on('click', function() {
				$(".tree-menu a.active").removeClass("active");
				$this = $(this);
				$this.addClass("active");
				var vars = {};
				vars.id = $this.attr('node');
				send_ajax("{:url('read')}", vars, function(ret) {
					show_data(ret);
				});
				return false;
			});

			function show_data(ret) {
				for (var s in ret.data) {
					set_val(s, ret.data[s]);
				}
				$("#dept_name").val($("#dept_list option[value='" + $("#id").val() + "']").text());
				$("#opmode").val("edit");
			};
		});
	</script>
</block>